package com.lj.api.v1.c;


import com.alibaba.fastjson.JSONObject;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.Result;
import com.lj.mcore.utils.PageBean;
import com.lj.mcore.entity.User;
import com.lj.mcore.service.IUserService;
import com.lj.mcore.utils.CommUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/c")
public class UserController {

    @Autowired
    IUserService userService;


    @RequestMapping("/userinfo")
    public String userInfo(long id){
        if (id<=0){
            return Result.error(CodeMsg.PARAMETER_ISNULL);
        }
        User user = userService.getObjById(id);
        if (user==null){
            return Result.error(CodeMsg.PARAMETER_ISNULL);
        }
        Map map = new HashMap();
        map.put("uid",user.getId());
        map.put("roles",user.getUserRole().split(","));
        map.put("name",user.getTrueName());
        return Result.ok(map);
    }

    @RequestMapping("/user_list")
    public String userList(String currentPage,String userName,String trueName,String email){
        int page = CommUtils.null2Int(currentPage)==0?1:CommUtils.null2Int(currentPage);
        Map query = new HashMap();
        query.put("userName",userName);
        query.put("trueName",trueName);
        query.put("email",email);
        PageBean pagebean = userService.getUserList(15,page,query);
        List<User> list = pagebean.getList();

        List list1 = new ArrayList();
        for(User user:list){
            Map map = new HashMap();
            map.put("addTime",CommUtils.formatTime("yyyy-MM-dd HH:mm:ss",user.getAddTime()));
            map.put("qq",user.getQQ());
            map.put("email",user.getEmail());
            map.put("uid",user.getId());
            map.put("userName",user.getUserName());
            map.put("trueName",user.getTrueName());
            map.put("integral",user.getIntegral());
            map.put("gold",user.getGold());
            map.put("loginCount",user.getLoginCount());
            map.put("lastLoginIp",CommUtils.formatTime("yyyy-MM-dd HH:mm:ss",user.getLastLoginDate()));
            map.put("lastLoginIp",user.getLastLoginIp());
            map.put("availableBalance",user.getAvailableBalance());
            map.put("freezeBlance",user.getFreezeBlance());
            map.put("sid",user.getStore()!=null?user.getStore().getId():0);
            list1.add(map);
        }

        Map info = new HashMap();
        info.put("objs", JSONObject.toJSON(list1));
        info.put("totalPage", pagebean.getTotalPage());
        info.put("pageSize", pagebean.getPageSize());
        info.put("rows", pagebean.getAllRow());
        info.put("currentPage", pagebean.getCurrentPage());
            return Result.ok(info);
    }



}
